library(lme4)
library(interplot)
paradiplomacy <- read.csv("Paradiplomacy_Russia.csv")

#negative binomial models (Table 1)

model1 <- glmer.nb(meetings_adj ~ varyag + transfers_share + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model2 <- glmer.nb(meetings_adj ~ varyag + transfers_share + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model3 <- glmer.nb(meetings_adj ~ varyag*tenure + transfers_share + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model4 <- glmer.nb(visits_adj ~ varyag + transfers_share + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")


model5 <- glmer.nb(visits_adj ~ varyag + transfers_share + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model6 <- glmer.nb(visits_adj ~ varyag*tenure + transfers_share + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

#negative binomial models (Appendix 4)

model1 <- glmer.nb(meetings_adj ~ varyag + log(transfers_10000) + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model2 <- glmer.nb(meetings_adj ~ varyag + log(transfers_10000) + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model3 <- glmer.nb(meetings_adj ~ varyag*tenure + log(transfers_10000) + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model4 <- glmer.nb(visits_adj ~ varyag + log(transfers_10000) + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")


model5 <- glmer.nb(visits_adj ~ varyag + log(transfers_10000) + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model6 <- glmer.nb(visits_adj ~ varyag*tenure + log(transfers_10000) + log(grp) + democracy + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

#models with the components of democracy index (Table 2)
#pluralism
model7 <- glmer.nb(meetings_adj ~ pluralism + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model8 <- glmer.nb(visits_adj ~ pluralism + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

#transparency of politics
model9 <- glmer.nb(meetings_adj ~ openness + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model10 <- glmer.nb(visits_adj ~ openness + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")


#fairness of elections
model11 <- glmer.nb(meetings_adj ~ fairness + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model12 <- glmer.nb(visits_adj ~ fairness + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

#media freedom
model13 <- glmer.nb(meetings_adj ~ media + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model14 <- glmer.nb(visits_adj ~ media + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

#liberalization of economy 
model15 <- glmer.nb(meetings_adj ~ liberalization + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model16 <- glmer.nb(visits_adj ~ liberalization + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

#civil society
model17 <- glmer.nb(meetings_adj ~ civil.society + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model18 <- glmer.nb(visits_adj ~ civil.society + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")                   

#political structure
model19 <- glmer.nb(meetings_adj ~ regime + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model20 <- glmer.nb(visits_adj ~ regime + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")  

#elites
model21 <- glmer.nb(meetings_adj ~ elites + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model22 <- glmer.nb(visits_adj ~ elites + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")  

#corruption
model23 <- glmer.nb(meetings_adj ~ corruption + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model24 <- glmer.nb(visits_adj ~ corruption + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")  

#local self-governance
model25 <- glmer.nb(meetings_adj ~ municipal + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit")

model26 <- glmer.nb(visits_adj ~ municipal + varyag + transfers_share + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = paradiplomacy,
                   na.action = "na.omit") 

#interaction of pluralism and transfers
model27 <- glmer.nb(visits_adj ~ pluralism*transfers_share + varyag + log(grp) + ethnicity + urabnization + resources + republic + border + north_west + new_distance + consulates + year + (1|region/governor),
                    data = paradiplomacy,
                    na.action = "na.omit") 
#interplot
interplot(model27, var1 = "pluralism", var2 = "transfers_share",hist = TRUE) +
  aes(color = "pink") + theme(legend.position="none") + 
  geom_hline(yintercept = 0, linetype = "dashed") 

#Predictors of the republican international activity (Table 4)
republics <- subset(paradiplomacy, republic == 1)

model28 <- glmer.nb(meetings_adj ~ separatism + lang_prof + ethnicity + democracy + log(grp) + urbanization + resources + border + north_west + new_distance + consulates + year + (1|region/governor),
                          data = republics,
                          na.action = "na.omit")

model29 <- glmer.nb(visits_adj ~ separatism + lang_prof + ethnicity + democracy + log(grp) + urbanization + resources + border + north_west + new_distance + consulates + year + (1|region/governor),
                   data = republics,
                   na.action = "na.omit")

